<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>div跟随</title>
    <style>
        #box1{
            width: 100px;
            height: 100px;
            background-color: red;
            /*
            开启box1的绝对定位
             */
            position: absolute;
        }
    </style>
    <script>
        window.onload=function(){
        //使div跟随鼠标移动
            var box1=document.getElementById("box1");
            document.onmousemove=function (event){
                var left=event.pageX;
                var top=event.pageY;

                //设置Div的偏移量
                box1.style.left=left +"px";
                box1.style.top=top +"px";
            }
        }
    </script>
</head>
<body>
<div id="box1"></div>
</body>
</html>